package com.learn.rpc.socket;

import com.learn.dubbo.pojo.User;
import com.learn.rpc.common.AdminUser;
import com.learn.rpc.common.AdminUserServiceImpl;
import com.learn.rpc.common.IAdminUserService;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * RPC服务端的第一版实现,使用ServerSocket进行数据传递,将一台服务器上的数据通过Socket连接传递到另一台数据
 * @author wangxing
 * @version 2020/8/14 17:01 Administrator
 */
public class RpcServer {
    private static boolean running = true;
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8899);
        while(running){
            Socket socket = serverSocket.accept();
            process(socket);
            socket.close();
        }
    }
    private static void process(Socket socket) throws IOException {
        InputStream in =  socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        DataInputStream dataInputStream = new DataInputStream(in);
        DataOutputStream dataOutputStream = new DataOutputStream(out);
        int id = dataInputStream.readInt();
        System.out.println("read id is "+ id);
        IAdminUserService service = new AdminUserServiceImpl();
        AdminUser adminUser = service.findAdminUserById(id);
        dataOutputStream.writeInt(adminUser.getId());
        dataOutputStream.writeUTF(adminUser.getName());
        dataOutputStream.flush();

        in.close();
        out.close();
        dataInputStream.close();
        dataOutputStream.close();
    }
}
